<?php
namespace app\admin\controller;
use think\Controller;
/**
 * 图片上传
 * Class Upload
 * @package app\admin\controller
 */
class UploadController extends Controller {

	protected function _initialize() {
		parent::_initialize();
		if (!session('admin_id')) {
			$this -> error('未登录');
		}
	}

	/**
	 * 单图片上传
	 */
	public function index() {
		$file = request() -> file('upload');
		$info = $file -> move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'image');
		if ($info) {
			$result['code'] = 1;
			$result['msg'] = '上传成功！';
			$result['src'] = '/public/uploads/image/' . str_replace('\\', '/', $info -> getSaveName());
		} else {
			$result['code'] = 0;
			$result['msg'] = $file -> getError();
		}
		return json($result);
	}

}
